package com.innovations.inn.service.impl;

import static com.innovations.common.utils.MyBeanUtils.copyProperties;
import com.innovations.common.utils.uuid.IdGeneratorSnowflake;
import com.innovations.inn.domain.CodeSystem;
import com.innovations.inn.domain.ValueSet;
import com.innovations.inn.domain.ValueSetInfo;
import com.innovations.inn.domain.response.ResponseValueSetInfoCount;
import com.innovations.inn.mapper.CodeSystemMapper;
import com.innovations.inn.mapper.ValueSetMapper;
import com.innovations.inn.service.ICodeSystemService;
import com.innovations.inn.service.IValueSetService;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 字典分类Service业务层处理
 *
 * @author dp
 * @date 2024-11-01
 */
@Service
public class CodeSystemServiceImpl implements ICodeSystemService {
    @Autowired
    CodeSystemMapper codeSystemMapper;
    @Autowired
    IdGeneratorSnowflake idGeneratorSnowflake;
    @Autowired
    IValueSetService valueSetService;
    @Autowired
    ValueSetMapper valueSetMapper;

    /**
     * 查询字典分类
     *
     * @param codeSystemId 字典分类主键
     * @return 字典分类
     */
    @Override
    public CodeSystem selectCodeSystemByCodeSystemId(Long codeSystemId) {
        return codeSystemMapper.selectCodeSystemByCodeSystemId(codeSystemId);
    }

    /**
     * 查询字典分类列表
     *
     * @param codeSystem 字典分类
     * @return 字典分类
     */
    @Override
    public List<CodeSystem> selectCodeSystemList(CodeSystem codeSystem) {
        return codeSystemMapper.selectCodeSystemList(codeSystem);
    }

    /**
     * 新增字典分类
     *
     * @param codeSystem 字典分类
     * @return 结果
     */
    @Override
    public int insertCodeSystem(CodeSystem codeSystem) {
        return codeSystemMapper.insertCodeSystem(codeSystem);
    }

    /**
     * 修改字典分类
     *
     * @param codeSystem 字典分类
     * @return 结果
     */
    @Override
    public int updateCodeSystem(CodeSystem codeSystem) {
        return codeSystemMapper.updateCodeSystem(codeSystem);
    }

    /**
     * 批量删除字典分类
     *
     * @param codeSystemIds 需要删除的字典分类主键
     * @return 结果
     */
    @Override
    public int deleteCodeSystemByCodeSystemIds(Long[] codeSystemIds) {
        return codeSystemMapper.deleteCodeSystemByCodeSystemIds(codeSystemIds);
    }

    /**
     * 删除字典分类信息
     *
     * @param codeSystemId 字典分类主键
     * @return 结果
     */
    @Override
    public int deleteCodeSystemByCodeSystemId(Long codeSystemId) {
        return codeSystemMapper.deleteCodeSystemByCodeSystemId(codeSystemId);
    }

    /**
     * 查询字典分类对应明细
     *
     * @param codeSystem 字典分类
     * @return 字典分类集合
     */
    @Override
    public List<ValueSetInfo> selectCodeSystemListValue(CodeSystem codeSystem) {
        // 判断字典分类标识是否为空
        if (codeSystem.getCodeSystemId() == null) {
            List<CodeSystem> codeSystemList = codeSystemMapper.selectCodeSystemList(codeSystem);
            if (codeSystemList.isEmpty()) {
                return null;
            } else {
                codeSystem.setCodeSystemId(codeSystemList.get(0).getCodeSystemId());
            }
        }
        // 设置查询字典明细条件
        ValueSet valueSet = new ValueSet();
        valueSet.setCodeSystemId(codeSystem.getCodeSystemId());
        // 查询字典明细
        List<ValueSet> list = valueSetService.selectValueSetList(valueSet);
        // 查询结果‘类型转换’
        return list.stream().map(ls ->
                copyProperties(ls, ValueSetInfo.class)
        ).collect(Collectors.toList());
    }

    /**
     * 查询字典分类对应明细计数
     *
     * @param codeSystem 字典分类
     * @return 字典分类计数集合
     */
    @Override
    public List<ResponseValueSetInfoCount> selectCodeSystemListValueCount(CodeSystem codeSystem) {
        if (Objects.equals(codeSystem.getCodeSystemNo(), "time_type")) {
            return valueSetMapper.selectCodeSystemListValueCountTime(codeSystem.getCodeSystemNo());
        } else {
            return valueSetMapper.selectCodeSystemListValueCount(codeSystem.getCodeSystemNo());
        }
    }

    /**
     * 实体数据补全
     *
     * @param codeSystem 实体
     * @param userId     用户id
     * @param crateType  创建类型 1：新增 2：修改
     */
    @Override
    public void completeData(CodeSystem codeSystem, Long userId, Integer crateType) {
//        // 标识
//        if (codeSystem.getTargetId() == null || "".equals(codeSystem.getTargetId())) {
//            codeSystem.setTargetId(idGeneratorSnowflake.nextId());
//        }

        // 创建模式
        if (crateType == 1) {
            // 创建人、创建时间
        }
        // 修改模式
        else if (crateType == 2) {

        }


    }

}
